package windows;

import java.awt.BorderLayout;
import java.awt.ComponentOrientation;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputMethodEvent;
import java.awt.event.InputMethodListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Observable;
import java.util.Observer;

import javaUtils.DateUtils;

import javax.swing.JButton;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import javax.swing.SwingConstants;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Document;

import langBundle.MsgControl;

import sqlServer.InstalledMenus;
import sqlServer.Menus;
import windowUnits.TextFieldWithDatePicker;

//import com.qt.datapicker.DatePicker;
import com.swtdesigner.SwingResourceManager;

public class MenuEdit extends JPanel {

	private SpringLayout springLayout;
	private final JPanel menuTopPanel = new JPanel();
	private final JLabel menuTopLabel = new JLabel();
	private final JPanel menuCenterPanel = new JPanel();
	private final JLabel menuTitleLabel = new JLabel();
	private final JTextField menuTitleField = new JTextField();
	private final JLabel menuPositionLabel = new JLabel();
	private final JComboBox menuPositionCombo = new JComboBox();
	private final TextFieldWithDatePicker datePublichedFrom = new TextFieldWithDatePicker();
	private final JLabel menuPublichedFromLabel = new JLabel();
	
	private final JLabel publichedToLabel = new JLabel();
	private final JTextField textField = new JTextField();
	private final TextFieldWithDatePicker datePublichedTo = new TextFieldWithDatePicker();
	private Menus menuEdit;
	private HashMap <Integer, InstalledMenus> installedMenusMap;
	private MsgControl msgBundle = null;
	/**
	 * Create the panel
	 */
	public MenuEdit(HashMap <Integer, InstalledMenus> installedMenusMap,MsgControl msgBundle) {
		super();
		this.installedMenusMap = installedMenusMap;
		this.msgBundle = msgBundle; 
		try {
			jbInit();
		} catch (Throwable e) {
			e.printStackTrace();
		}
		//
	}
	private void jbInit() throws Exception {
		setLayout(new BorderLayout());
		
		add(menuTopPanel, BorderLayout.NORTH);
		final FlowLayout flowLayout = new FlowLayout();
		flowLayout.setAlignment(FlowLayout.RIGHT);
		menuTopPanel.setLayout(flowLayout);
		menuTopPanel.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
		
		menuTopPanel.add(menuTopLabel);
		menuTopLabel.setHorizontalTextPosition(SwingConstants.LEFT);
		menuTopLabel.setText("Menu Edit");
		
		add(menuCenterPanel, BorderLayout.CENTER);
		springLayout = new SpringLayout();
		menuCenterPanel.setLayout(springLayout);
		
		menuCenterPanel.add(menuTitleLabel);
		springLayout.putConstraint(SpringLayout.NORTH, menuTitleLabel, 55, SpringLayout.NORTH, menuCenterPanel);
		springLayout.putConstraint(SpringLayout.WEST, menuTitleLabel, 40, SpringLayout.WEST, menuCenterPanel);
		menuTitleLabel.setText("Menu Title");
		
		menuCenterPanel.add(menuTitleField);
		menuTitleField.addKeyListener(new MenuTitleFieldKeyListener());
		menuTitleField.getDocument().addDocumentListener(new MyDocumentListener());
		menuTitleField.getDocument().putProperty("item_name", Menus.MENU_NAME);
		springLayout.putConstraint(SpringLayout.EAST, menuTitleField, 180, SpringLayout.WEST, menuCenterPanel);
		springLayout.putConstraint(SpringLayout.WEST, menuTitleField, 0, SpringLayout.WEST, menuTitleLabel);
		springLayout.putConstraint(SpringLayout.NORTH, menuTitleField, 5, SpringLayout.SOUTH, menuTitleLabel);
		
		menuCenterPanel.add(menuPositionLabel);
		springLayout.putConstraint(SpringLayout.NORTH, menuPositionLabel, 120, SpringLayout.NORTH, menuCenterPanel);
		springLayout.putConstraint(SpringLayout.WEST, menuPositionLabel, 0, SpringLayout.WEST, menuTitleField);
		menuPositionLabel.setText("Menu Position");
		
		menuCenterPanel.add(menuPositionCombo);
		menuPositionCombo.addItemListener(new MenuPositionComboItemListener());
		springLayout.putConstraint(SpringLayout.EAST, menuPositionCombo, 0, SpringLayout.EAST, menuTitleField);
		springLayout.putConstraint(SpringLayout.WEST, menuPositionCombo, 0, SpringLayout.WEST, menuPositionLabel);
		menuPositionCombo.setActionCommand("");
		springLayout.putConstraint(SpringLayout.NORTH, menuPositionCombo, 5, SpringLayout.SOUTH, menuPositionLabel);
		
		menuCenterPanel.add(menuPublichedFromLabel);
		springLayout.putConstraint(SpringLayout.NORTH, menuPublichedFromLabel, 190, SpringLayout.NORTH, menuCenterPanel);
		springLayout.putConstraint(SpringLayout.WEST, menuPublichedFromLabel, 0, SpringLayout.WEST, menuPositionCombo);
		menuPublichedFromLabel.setText("Publiched From");
		
		menuCenterPanel.add(datePublichedFrom);
		datePublichedFrom.getDocument().addDocumentListener(new MyDocumentListener());
		datePublichedFrom.getDocument().putProperty("item_name", Menus.PUBLICHED_FROM);
		springLayout.putConstraint(SpringLayout.EAST, datePublichedFrom, 0, SpringLayout.EAST, menuPositionCombo);
		springLayout.putConstraint(SpringLayout.WEST, datePublichedFrom, 0, SpringLayout.WEST, menuPublichedFromLabel);
		springLayout.putConstraint(SpringLayout.NORTH, datePublichedFrom, 5, SpringLayout.SOUTH, menuPublichedFromLabel);
		//datePublichedFrom.setColumns(10);
		datePublichedFrom.setText(DateUtils.getDateTime());
		
		menuCenterPanel.add(publichedToLabel);
		springLayout.putConstraint(SpringLayout.NORTH, publichedToLabel, 250, SpringLayout.NORTH, menuCenterPanel);
		springLayout.putConstraint(SpringLayout.WEST, publichedToLabel, 0, SpringLayout.WEST, menuPublichedFromLabel);
		publichedToLabel.setText("Publiched To");
		
		menuCenterPanel.add(datePublichedTo);
		datePublichedTo.getDocument().addDocumentListener(new MyDocumentListener());
		datePublichedTo.getDocument().putProperty("item_name", Menus.PUBLICHED_TO);
		springLayout.putConstraint(SpringLayout.WEST, datePublichedTo, 0, SpringLayout.WEST, publichedToLabel);
		springLayout.putConstraint(SpringLayout.EAST, datePublichedTo, 0, SpringLayout.EAST, menuPositionCombo);
		springLayout.putConstraint(SpringLayout.NORTH, datePublichedTo, 5, SpringLayout.SOUTH, publichedToLabel);
		
	}
	
	private class MenuTitleFieldKeyListener extends KeyAdapter {
		public void keyTyped(KeyEvent arg0) {
			menuTitleField_keyTyped(arg0);
		}
	}
	private class MenuPositionComboItemListener implements ItemListener {
		public void itemStateChanged(ItemEvent arg0) {
			menuPositionCombo_itemStateChanged(arg0);
		}
	}
	private class MyDocumentListener implements DocumentListener {
	    String newline = "\n";
	 
	    public void insertUpdate(DocumentEvent e) {
	        updateLog(e, "inserted into");
	    }
	    public void removeUpdate(DocumentEvent e) {
	        updateLog(e, "removed from");
	    }
	    public void changedUpdate(DocumentEvent e) {
	        //Plain text components do not fire these events
	    	//updateLog(e, "updated from");
	    }

	    public void updateLog(DocumentEvent e, String action) {
	    	//JOptionPane.showMessageDialog(null,"changed "+action);
	    	Document doc = (Document)e.getDocument();
	    	if(doc.getProperty("initialized") != null)
	    		if((Boolean)doc.getProperty("initialized"))
	    			updateItem((Integer)doc.getProperty("item_name"));
			//JOptionPane.showMessageDialog(this,"- "+this.getParent().getParent().getComponentCount());
			
	    }
	}

	public void setMenuToEdit(Menus menuEdit){
		this.menuEdit = menuEdit;
		menuTitleField.getDocument().putProperty("initialized", false);
		datePublichedFrom.getDocument().putProperty("initialized", false);
		datePublichedTo.getDocument().putProperty("initialized", false);
		menuPositionCombo.setActionCommand("0");
		fillForms();
	}
	private void fillForms(){
		menuTitleField.setText(menuEdit.getMenuItem(Menus.MENU_NAME));
		menuTitleField.getDocument().putProperty("initialized", true);
		menuPositionCombo.removeAllItems();
		for(int i=0;i<installedMenusMap.size();i++){
			// installedMenusMap.get(i)
			menuPositionCombo.addItem(installedMenusMap.get(i+1));
			
			if(installedMenusMap.get(i+1).getParam(InstalledMenus.MENU_ID).equals(menuEdit.getMenuItem(Menus.MENU_TYPE_ID))){
				menuPositionCombo.setSelectedIndex(i);
			}
		}
		menuPositionCombo.setActionCommand("1");
		datePublichedFrom.setText(menuEdit.getMenuItem(Menus.PUBLICHED_FROM));
		datePublichedFrom.getDocument().putProperty("initialized", true);
		datePublichedTo.setText(menuEdit.getMenuItem(Menus.PUBLICHED_TO));
		datePublichedTo.getDocument().putProperty("initialized", true);
		
		if(Integer.parseInt(menuEdit.getMenuItem(Menus.PUBLICHED)) == 1){
		  menuTopLabel.setIcon(SwingResourceManager.getIcon(MenuEdit.class, "/icons/Globe33.png"));
		  msgBundle.setMsgByName(menuTopLabel,"published");
		}else{
		  menuTopLabel.setIcon(SwingResourceManager.getIcon(MenuEdit.class, "/icons/unpublich.png"));
		  msgBundle.setMsgByName(menuTopLabel,"unpubliched");
		}
		menuTopLabel.repaint();
	}
	
	
	public boolean isMenuChanged(){
		boolean changed = false;
		
		if(!menuTitleField.getText().equals(menuEdit.getMenuItem(Menus.MENU_NAME)))
			changed = true;
		else if(!((InstalledMenus)menuPositionCombo.getSelectedItem()).getParam(InstalledMenus.MENU_ID).equals(menuEdit.getMenuItem(Menus.MENU_TYPE_ID)) && menuPositionCombo.getItemCount()>0)
			changed = true;
		else if(!datePublichedFrom.getText().equals(menuEdit.getMenuItem(Menus.PUBLICHED_FROM)))
			changed = true;
		else if(!datePublichedTo.getText().equals(menuEdit.getMenuItem(Menus.PUBLICHED_TO)))
			changed = true;
		
		return changed;
	}
	protected void menuTitleField_keyTyped(KeyEvent arg0) {
		/*//this.getParent().getComponentCount()	
		menuEdit.setMenuChanged(isMenuChanged());
		menuEdit.addMenuHisotry(menuEdit);
		menuEdit.setMenu(Menus.MENU_NAME, ((JTextField)arg0.getComponent()).getText()+arg0.getKeyChar());
		//JOptionPane.showMessageDialog(this,"- "+this.getParent().getParent().getComponentCount());
		PageContent pc = (PageContent)this.getParent().getParent().getParent().getParent();
		pc.repaintTree();
		*/
	}
	protected void menuPositionCombo_itemStateChanged(ItemEvent arg0) {
		if(menuPositionCombo.getActionCommand() != null)
			if(menuPositionCombo.getActionCommand().equals("1")){
				menuEdit.setChanged(isMenuChanged());
				menuEdit.addMenuHisotry(menuEdit);
				menuEdit.setMenu(Menus.MENU_TYPE_ID,Integer.parseInt(((InstalledMenus)menuPositionCombo.getSelectedItem()).getParam(InstalledMenus.MENU_ID)));
				menuEdit.setMenu(Menus.MENU_POSITION,((InstalledMenus)menuPositionCombo.getSelectedItem()).getParam(InstalledMenus.MENU_POSITION));
				repaintTree();
			}
	}
	protected void updateItem(int item){
		System.out.println("Update: "+item+"/"+isMenuChanged());
		if(isMenuChanged())
			menuEdit.addMenuHisotry(menuEdit);
		if(item == Menus.MENU_NAME){
			menuEdit.setMenu(Menus.MENU_NAME, menuTitleField.getText());
		}else if(item == Menus.PUBLICHED_FROM){
			menuEdit.setMenu(Menus.PUBLICHED_FROM, datePublichedFrom.getText());
		}else if(item == Menus.PUBLICHED_TO){
			menuEdit.setMenu(Menus.PUBLICHED_TO, datePublichedTo.getText());
		}
		menuEdit.setChanged(isMenuChanged());
		
		repaintTree();
		
	}
	private void repaintTree(){
		if(this.getParent() != null){
			PageContent pc = (PageContent)this.getParent().getParent().getParent().getParent().getParent();
			pc.repaintTree();
		}
	}
	
}
